/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.List;

import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.model.DetailedProductModel;
import com.elf.gwt.common.client.model.ProductModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.utils.StoreUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;


/**
 * 
 */

public class CartSummaryComponent extends StoreSimpleComponent
{
	protected Grid bodyPanel;
	protected String multiplier;
	protected CartComponent cartComponent;
	protected DialogBox dialogBox = null;
	protected Image dialogImage = null;
	/**
	 * Constructor
	 */

	public CartSummaryComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, "arrow_right.gif", true);
		cartComponent = (CartComponent)controller.getComponent("CartComponent");
		multiplier = controller.getStoreConstants().CartSummaryComponentMultiplier();
		getRightTitleImage().addClickHandler(cartClickHandler);
	}

	public void onBuilt()
	{
		List<DetailedProductModel> productModels = cartComponent.getCartModel().getProducts();
		int sz = productModels.size();
		if (sz > 0) {
			setVisible(true);
		}
		else {
			setVisible(false);
		}
	}
	public void build()
	{		
		List<DetailedProductModel> productModels = cartComponent.getCartModel().getProducts();
		int sz = productModels.size();
		bodyPanel = new Grid(sz, 2);
		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		bodyPanel.addClickHandler(tableHandler);
		CellFormatter cf = bodyPanel.getCellFormatter();
		for (int i = 0; i < sz; i++)
		{
			DetailedProductModel cartProductModel = productModels.get(i);

			InlineLabel inline = new InlineLabel(cartProductModel.getName());
			inline.addStyleName("index:" + i);
			inline.addMouseOverHandler(mouseOverHandler);
			inline.addMouseOutHandler(mouseOutHandler);
			bodyPanel.setText(i, 0, String.valueOf(cartProductModel.getCartQuantity()) + multiplier);
			bodyPanel.setWidget(i, 1, inline);
			cf.addStyleName(i, 0, getHandle() + "Number");
			cf.addStyleName(i, 1, getHandle() + "Name");
			cf.addStyleName(i, 1, "ComponentLink");
		}

		panel.setWidget(1, 0, bodyPanel);

		onBuilt();
	}

	protected ClickHandler tableHandler = new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			if (dialogBox != null  && dialogBox.isShowing()) dialogBox.hide();
			HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
			int index = cell.getRowIndex();
			ProductModel productModel = cartComponent.getCartModel().getProducts().get(index);
			onSelection("ProductComponent--" + productModel.getId() + "--" + productModel.getCategoryId() + "--0");
		}
	};
	
	protected ClickHandler cartClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			onSelection("CartComponent");
		}
	};

	protected MouseOverHandler mouseOverHandler = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event)
		{
			Widget source = (Widget)event.getSource();
			String stylenames = source.getStyleName();
			int loc = stylenames.indexOf("index:");
			stylenames = stylenames.substring(loc+6);
			loc = stylenames.indexOf(" ");
			if (loc > -1) stylenames = stylenames.substring(0, loc);
			int id = Integer.parseInt(stylenames);
			ProductModel productModel = cartComponent.getCartModel().getProducts().get(id);
			String imagename = productModel.getImage();
			if (dialogBox == null) {
				dialogBox = new DialogBox();
				dialogBox.setStyleName(getHandle() + "DialogBox");
				dialogBox.setModal(false);
				dialogImage = new Image();
				dialogBox.add(dialogImage);
			}
			dialogImage.setUrl(StoreUtils.getImageProductsDirectory() + imagename);
			String[] imageSize = productModel.getImageSize();
			imageSize = AbstractModel.adjustedImageSize(
					Double.valueOf(imageSize[0]).doubleValue(), 
					Double.valueOf(imageSize[1]).doubleValue(), 
					StoreComponentController.categoriesComponent.categoryMetaModel.getSmallImageWidthMax(), 
					StoreComponentController.categoriesComponent.categoryMetaModel.getSmallImageHeightMax());
			int width = Integer.parseInt(imageSize[0]) * 2;
			int height = Integer.parseInt(imageSize[1]) * 2;
			dialogImage.setSize(String.valueOf(width), String.valueOf(height));
			dialogBox.center();
			dialogBox.show();
		}
	};

	protected MouseOutHandler mouseOutHandler = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event)
		{
			if (dialogBox != null  && dialogBox.isShowing()) dialogBox.hide();
		}
	};
}
